/**
 * Created by Arterli on 2015/4/16.
 */
var fs = require('fs');
var images = require("images");
module.exports = Controller("Admin/BaseController", function () {
    "use strict";
    return {
        indexAction: function () {
            //文章管理
            var self = this;
            var cate = D('category').select().then(function (data) {
                return get_children(data, 0);
            });
            this.assign({
                success: self.get('success') == 1 ? "1" : "0",
                category: cate,
                active: 'articleindex'//当前高亮
            });
            this.display();

        },
        draftAction: function () {
            //草稿箱
            var self = this;
            var cate = D('category').select().then(function (data) {
                return get_children(data, 0);
            });
            this.assign({
                success: self.get('success') == 1 ? "1" : "0",
                category: cate,
                active: 'draft'//当前高亮
            });
            this.display();

        },
        todraftAction: function () {//移动到草稿箱
            var self = this;
            if (this.isAjax('get')) {
                var gid = this.get('gid');
                gid = gid.split(",");
                console.log(gid);
                var hide = this.get('hide');
                console.log(gid)
                D('article').where({gid: ['IN', gid]}).update({
                    hide: hide
                }).then(function (data) {
                    self.json(data);
                })

            }
        },
        deleAction: function () {
            //删除文章
            var self = this;
            if (this.isAjax('get')) {
                var gid = this.get('gid');
                gid = gid.split(",")
                if (gid.length > 1) {
                    var gids = [];
                    gid.forEach(function (item) {
                        gids.push(item);

                    });
                    var where = {gid: ["IN", gids]}
                    var gidd = gids;
                } else {
                    where = {gid: gid[0]}
                    gidd = gid;
                }
                if (this.get('gid')) {

                    return D('article').where(where).delete().then(function (row) {
                        gidd.forEach(function (gtm) {

                            D('tag').where({gid: ['like', '%{' + gtm + '}%']}).select().then(function (data) {
                                console.log(data);
                                var gid = '{' + gtm + '';
                                console.log(gid + "dddddd");
                                data.forEach(function (item) {
                                    console.log(item.gid)
                                    var index = item.gid.indexOf(gid)
                                    var isdh = item.gid.slice(index - 1, index);
                                    if (isdh == ",") {
                                        console.log("有，")
                                        var ngid = item.gid.replace("," + gid, "")
                                        console.log(ngid)
                                    } else {
                                        console.log("没有，")
                                        ngid = item.gid.replace(gid, "")
                                        console.log(ngid)
                                    }
                                    console.log(item);
                                    D('tag').where({tid: item.tid}).update({gid: ngid}).then(function (d) {
                                        console.log(d);
                                    });
                                })

                            });
                        })


                        self.json(row);
                    })
                } else {
                    this.end();
                }


            }
        },

        dataAction: function () {

            var self = this;
            var page = this.get("offset") > 1 ? (this.get("offset") / 20) + 1 : 1;
            // console.log(page)
            var limit = this.get("limit");
            var search = this.get('search').length > 0 ? this.get('search') : "";
            var where = {title: ['like', '%' + search + '%']}
            if (this.get("cid")) {
                where.cateid = this.get("cid");
            }
            if (this.get("hide")) {
                where.hide = this.get("hide");
            }
            //console.log(where);
            return D('article').field('gid,title,username,date,catename,attnum,icon')
                .join({
                    table: 'user',
                    join: 'left', //join方式，有 left, right, inner 3种方式
                    as: 'u', //表别名
                    on: ['author', 'uid'] //ON条件
                }).join({
                    table: 'category',
                    join: 'left', //join方式，有 left, right, inner 3种方式
                    as: 'c', //表别名
                    on: ['cateid', 'cid'] //ON条件
                }).page(page, limit).order('date desc').where(where).countSelect().then(function (da) {
                    //data的数据格式为
                    var datas = {};
                    datas.total = da.count;

                    var rows = [];
                    da.data.forEach(function (item) {
                        var time = times(item.date);
                        var catename = item.catename ? '<i class="fa '+item.icon+'"></i> '  +item.catename : "未分类";
                        var titleico = item.attnum ? '<i class="fa fa-file-image-o"></i> ':'<i class="fa fa-file-text-o"></i> ';
                        rows.push({
                            date: time,
                            gid: item.gid,
                            title:titleico  + item.title + ' <a class="view ml10" href="' + item.gid + '" title="访问"><i class="fa fa-link"></i></a> ',
                            type: catename,
                            username: item.username
                        });


                    });

                    datas.rows = rows;
                    //console.log(datas)
                    self.json(datas)
                });

        },
        sqlAction: function () {
            var self = this;
            D('article').field('gid,title,username,date').join({
                table: 'user',
                join: 'inner', //join方式，有 left, right, inner 3种方式，
                as: 'u', //表别名
                on: ['author', 'uid'] //ON条件
            }).page(1, 2).countSelect().then(function (data) {
                console.log(data);
                self.end(data);

            });
        },
        addAction: function () {
            if (this.isPost()) {
                var param = this.post();
                param.date = new Date().valueOf();
                //console.log(param);
                var self = this;
                if(!param.attnum){param.attnum="noAttum"}
                D('attachment').field('aid').where({thumpath:['like','%'+param.attnum+'%']}).find().then(function(aid){
                    param.attnum=aid.aid;
                return D('article').add(param).then(function (id) {
                    if(aid){
                        D('attachment').where({aid:aid.aid}).update({blogid:id})
                    }
                    if (self.post('tags')) {
                        //var ids=id;
                        var tags = self.post('tags').split(",");
                        tags.forEach(function (item) {
                            var tag = {tagname: item, gid: '{' + id + '}'}
                            var where = {tagname: item}
                            return D('tag').thenAdd(tag, where, true).then(function (data) {
                                //console.log(data)
                                if (data.type == "exist") {
                                    D('tag').where({tid: data.id}).getField('gid', true).then(function (gid) {
                                        //ids=gid.push(data.id);
                                        var ids = gid + "," + '{' + id + '}';
                                        //console.log(ids)
                                        return D('tag').where({tid: data.id}).update({gid: ids}).then(function (ok) {
                                            //console.log(ok)
                                        })
                                    })
                                }

                            })
                        })
                    }
                    return self.redirect("/admin/article/index/success/1");
                });
                   });
            } else {

                var cate = D('category').select().then(function (data) {
                    return get_children(data, 0);
                });
                this.assign({
                    category: cate,
                    active: "article_deit" //当前高亮
                });
                this.display();
            }

        },
        editAction: function () {
            if (this.isPost()) {
                var param = this.post();
                //param.date = new Date().valueOf();
                console.log(param);
                var self = this;
                if(!param.attnum){param.attnum="noAttum"}
                D('attachment').field('aid').where({thumpath:['like','%'+param.attnum+'%']}).find().then(function(aid){
                    param.attnum=aid.aid;
                    if(aid){
                        D('attachment').where({aid:aid.aid}).update({blogid:param.gid})
                    }
                return D('article').where({gid: param.gid}).update(param).then(function (id) {
                    D('tag').where({gid: ['like', '%{' + param.gid + '}%']}).select().then(function (tags) {
                        function unique(arr) {//数组去重
                            var ret = []
                            var hash = {}

                            for (var i = 0; i < arr.length; i++) {
                                var item = arr[i]
                                var key = typeof(item) + item
                                if (hash[key] !== 1) {
                                    ret.push(item)
                                    hash[key] = 1
                                }
                            }

                            return ret
                        }

                        function unarr(arr1, arr2) {//删除重复
                            if (arr1.length > arr2.length) {
                                var len = arr1.length;
                                var larr = arr1;
                                var sarr = arr2;
                            } else {
                                len = arr2.length;
                                larr = arr2;
                                sarr = arr1;
                            }
                            var res = []
                            for (var i = 0; i < len; i++) {
                                if (!in_array(larr[i], sarr)) {
                                    res.push(larr[i]);
                                }
                            }
                            return res;
                        }

                        var n_tags = param.tags.split(",")
                        console.log("n_tags:" + n_tags)
                        var y_tags = []
                        tags.forEach(function (item) {
                            y_tags.push(item.tagname);
                        })
                        console.log("y_tags:" + y_tags)
                        var c_tags = y_tags.concat(n_tags)//合并数组
                        console.log("c_tags:" + c_tags)
                        var un_tags = unique(c_tags);//去重
                        console.log("un_tags:" + un_tags)
                        var ntags = unarr(un_tags, y_tags);//新增家的
                        console.log(ntags.length);
                        var ytags = unarr(un_tags, n_tags);//去除的
                        console.log(ytags.length);
                        if (ntags.length > 0) {//如果有新加的tag，直接添加
                            var tags = ntags;
                            console.log(tags);
                            tags.forEach(function (item) {
                                var tag = {tagname: item, gid: '{' + param.gid + '}'}
                                var where = {tagname: item}
                                return D('tag').thenAdd(tag, where, true).then(function (data) {
                                    //console.log(data)
                                    if (data.type == "exist") {
                                        D('tag').where({tid: data.id}).getField('gid', true).then(function (gid) {
                                            //ids=gid.push(data.id);
                                            var ids = gid + "," + '{' + param.gid + '}';

                                            console.log(ids)
                                            return D('tag').where({tid: data.id}).update({gid: ids}).then(function (ok) {
                                                return ok;
                                                //return self.redirect("/admin/article");
                                            })
                                        })
                                    }
                                    return data;
                                })
                            })
                        }

                        if (ytags.length) {
                            console.log(ytags);
                            D('tag').where({tagname: ['like', ytags]}).select().then(function (data) {
                                console.log(data);
                                var gid = '{' + param.gid + '}'
                                data.forEach(function (item) {
                                    console.log(item.gid)
                                    var index = item.gid.indexOf(gid)
                                    var isdh = item.gid.slice(index - 1, index);
                                    if (isdh == ",") {
                                        console.log("有，")
                                        var ngid = item.gid.replace("," + gid, "")
                                        console.log(ngid)
                                    } else {
                                        console.log("没有，")
                                        ngid = item.gid.replace(gid, "")
                                        console.log(ngid)
                                    }
                                    console.log(item);
                                    D('tag').where({tid: item.tid}).update({gid: ngid}).then(function (d) {
                                        console.log(d);
                                    });
                                })

                            });

                        }

                    })
                    if (param.dar) {
                        return self.redirect("/admin/article/draft/success/1");
                    }
                    return self.redirect("/admin/article/index/success/1");
                });
                });
            } else {
                var self = this;
                var cate = D('category').select().then(function (data) {
                    return get_children(data, 0);
                });
                var data = D('article').alias('a')
                    .join({
                        table: 'category',
                        jion: 'left',
                        as: 'c',
                        on: ['cateid', 'cid']
                    }).join({
                        table: 'attachment',
                        jion: 'left',
                        as: 'd',
                        on: ['attnum', 'aid']
                    }).where({gid: self.get('gid')}).find().then(function (data) {
                        console.log(data)
                        data.date = times(data.date)
                        self.assign('data', data);
                        D('tag').where({gid: ['like', '%{'+ data.gid+'}%']}).select().then(function (tags) {
                            self.assign('tags', tags);
                            self.assign({
                                category: cate,
                                active: "article_deit" //当前高亮
                            });
                            self.display();
                        })
                    })

            }
        },
        imgsaveAction: function () {
            //原图保存
            var self = this;
            var img = this.file('img');//接收图片
            var oriName = img.originalFilename;//图片原名
            var filpath = img.path;//原图片路径
            var conf = F("ueditorconfig");//读取配置文件
            var config = {
                pathFormat: conf['imagePathFormat'],
                maxSize: conf['imageMaxSize'],
                allowFiles: conf['imageAllowFiles']
            }
            var fullname = getFullName(config, oriName);//获得新图片地址
            var filename = getFileName(fullname);//新图片名称
            console.log(filename);
            console.log(fullname);
            creatdir(fullname).then(function (res) {

                //保村图片
                savefile(filpath, fullname).then(function (res) {
                    var size = images(RESOURCE_PATH+"/"+res).size();
                    console.log(img)
                    var mimetype = img.headers;
                    var data = {}
                    data.filename = img.originalFilename;
                    data.filesize = img.size;
                    data.filepath = "/" + res;
                    data.addtime = new Date().valueOf();
                    data.width = size.width;
                    data.height = size.height;
                    data.mimetype = mimetype['content-type'];
                    return D('attachment').add(data).then(function (data) {
                        console.log(data);
                        self.json({
                            "status": "success",
                            "url": "/" + res,
                            "width": size.width,
                            "height": size.height
                        })
                    })

                });
            });

        },
        imgcropAction: function () {
            //缩略图裁剪
            var self = this;
            var img = this.post();
            var imgurl = img.imgUrl.substr(1),
                imginitw = parseInt(img.imgInitW),
                imginith = parseInt(img.imgInitH),
                imgw = parseInt(img.imgW),
                imgh = parseInt(img.imgH),
                imgy1 = parseInt(img.imgY1),
                imgx1 = parseInt(img.imgX1),
                croph = parseInt(img.cropH),
                cropw = parseInt(img.cropW),
                rotation = parseInt(img.rotation);
            var hz = imgurl.substr(imgurl.lastIndexOf("."));
            var cropurl = imgurl.replace(hz, "_" + cropw + "x" + croph + hz);
            //console.log(cropurl);
            var cropimg = function () {
                var deferred = getDefer();
                images(cropw, croph).fill(0xff, 0x00, 0x00, 0.5)
                    .draw(images(images(RESOURCE_PATH+"/"+imgurl).size(imgw), imgx1, imgy1, cropw, croph), 0, 0)
                    .save(RESOURCE_PATH+"/"+cropurl, {     //保存到output.jpg,图片质量为50
                        quality: 50
                    });
                deferred.resolve("/" + cropurl);
                return deferred.promise;
            }
            cropimg().then(function (rs) {
                return D('attachment').where({filepath: ['like', '%' + img.imgUrl + '%']}).update({thumpath:rs}).then(function (res) {
                    self.json({
                        "status": "success",
                        "url": rs
                    })
                })

            })

        }
    };
});